/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eit.eitsite.modules.sys.entity.User;
import com.zhuoyue.zhtj.common.Consts;
import com.zhuoyue.zhtj.common.ServerResponse;
import com.zhuoyue.zhtj.entity.Tyxx;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Stupicture;
import com.zhuoyue.zhtj.service.StupictureService;

import java.io.File;

/**
 * 学生照片Controller
 *
 * @author hss
 * @version 2017-09-08
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/stupicture")
public class StupictureController extends BaseController {

    @Autowired
    private StupictureService stupictureService;

    @ModelAttribute
    public Stupicture get(@RequestParam(required = false) String id) {
        Stupicture entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = stupictureService.get(id);
        }
        if (entity == null) {
            entity = new Stupicture();
        }
        return entity;
    }

    @RequiresPermissions("zhtj:stupicture:view")
    @RequestMapping(value = {"list", ""})
    public String list(Stupicture stupicture, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Stupicture> page = stupictureService.findPage(new Page<Stupicture>(request, response), stupicture);
        model.addAttribute("page", page);
        return "zhuoyue/zhtj/stupictureList";
    }

    @RequiresPermissions("zhtj:stupicture:view")
    @RequestMapping(value = "form")
    public String form(Stupicture stupicture, Model model) {
        model.addAttribute("stupicture", stupicture);
        return "zhuoyue/zhtj/stupictureForm";
    }

    @RequiresPermissions("zhtj:stupicture:edit")
    @RequestMapping(value = "save")
    public String save(Stupicture stupicture, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, stupicture)) {
            return form(stupicture, model);
        }
        stupictureService.save(stupicture);
        addMessage(redirectAttributes, "保存学生照片成功");
        return "redirect:" + Global.getAdminPath() + "/zhtj/stupicture/?repage";
    }

    @RequiresPermissions("zhtj:stupicture:edit")
    @RequestMapping(value = "delete")
    public String delete(Stupicture stupicture, RedirectAttributes redirectAttributes) {
        stupictureService.delete(stupicture);
        addMessage(redirectAttributes, "删除学生照片成功");
        return "redirect:" + Global.getAdminPath() + "/zhtj/stupicture/?repage";
    }

    @RequestMapping("getPictureByNum")
    public String getPictureByNum(Tyxx tyxx, HttpServletResponse response) {
        if (tyxx.getXh() != null && !"".equals(tyxx.getXh())) {
            String suffix = "userfiles/1/images/" + tyxx.getXh();
            String prefixLocal = getRequest().getSession().getServletContext().getRealPath("/");
            System.out.println(prefixLocal);
            String[] format = new String[]{"jpg","JPG", "png","PNG", "jpeg", "bmp","gif"};
            for (int i = 0; i < format.length; i++) {
                String tail = "." + format[i];
                String picture = prefixLocal + suffix + tail;
                System.out.println(picture);
                File file = new File(picture);
                if (file.exists()) {
                    picture = Consts.domainname + "/szptApp/" + suffix + tail;
                    System.out.println(picture);
                    return this.renderString(response, ServerResponse.createBySuccess("查询图片成功", picture));
                }
            }
            return this.renderString(response, ServerResponse.createByErrorMessage("此学号无图片"));
        }
        return this.renderString(response, ServerResponse.createByErrorMessage("学号不能为空"));
    }

}